package net.maku.generator.controller;

import lombok.AllArgsConstructor;
import net.maku.generator.common.utils.Result;
import net.maku.generator.service.FileUploadService;
import net.maku.generator.vo.UploadResourceVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * 通用文件上传下载
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping("gen/fileUpDownLoad")
@AllArgsConstructor
public class FileUpDownLoadController {
    private final FileUploadService fileUploadService;

    /**
     * 上传文件
     *
     * @param multipartFile  上传图片
     */
    @PostMapping("uploadFile/{fileType}")
    public Result<String> uploadFile(@PathVariable("fileType") String fileType, @RequestParam("file")MultipartFile multipartFile) {
        if (multipartFile.isEmpty()) {
            return Result.error("File is empty");
        }

        // 保存文件
        UploadResourceVO resourceVO = fileUploadService.uploadFile(fileType, multipartFile);
        return Result.ok(resourceVO.getUrlPath());
    }

    /**
     * 获得上传文件列表
     */
    @GetMapping("listFiles")
    public Result<List<String>> listFiles() {
        List<String> files = fileUploadService.getUploadedFiles();

        return Result.ok(files);
    }

}
